package com.n9.qq.android;

import com.n9.qq.android.mock.DeviceInfo;

import java.nio.ByteBuffer;
import java.util.Objects;

/**
 * @author ice
 * @since 2019/2/15 15:32
 */
public class ReqHead {
    private int requestId;
    private int appId;
    private String serviceCmd;
    private byte[] token;
    private byte[] cookie;
    private byte[] imei;
    private byte[] ksid = new byte[0];
    private String imsiAndSdkVersionReleaseTime = "|" + Const.IMSI + "|" + Const.REQ_HEAD_SDKVERSION;

    public ReqHead(String serviceCmd, AqContext context) {
        this(context.getRequestId(), Const.APPID, serviceCmd, DeviceInfo.imei.getBytes(), context.getToken010a(), context.getCookie());
    }

    /**
     * @param requestId
     * @param appId
     * @param serviceCmd
     * @param imei
     * @param token             010atoken
     * @param cookie
     */
    public ReqHead(int requestId, int appId, String serviceCmd, byte[] imei, byte[] token, byte[] cookie) {
        this.requestId = requestId;
        this.appId = appId;
        this.serviceCmd = serviceCmd;
        this.token = Objects.isNull(token) ? new byte[0] : token;
        this.imei = imei;
        this.cookie = Objects.isNull(cookie) ? new byte[0] : cookie;
    }

    public ReqHead(String serviceCmd, byte[] cookie) {
        this.serviceCmd = serviceCmd;
        this.cookie = cookie;
    }

    public byte[] getData() {
        if (Objects.isNull(imei)) {
            byte[] serviceCmdBytes = serviceCmd.getBytes();

            final int serviceCmdFullLen = serviceCmdBytes.length + 4;
            final int cookieFullLen = cookie.length + 4;
            int len = 4 + serviceCmdFullLen + cookieFullLen;

            ByteBuffer buf = ByteBuffer.allocate(len);
            buf.putInt(len);
            buf.putInt(serviceCmdFullLen);
            buf.put(serviceCmdBytes);
            buf.putInt(cookieFullLen);
            buf.put(cookie);

            return buf.array();
        } else {
            byte[] serviceCmdBytes = serviceCmd.getBytes();
            byte[] stringBytes = imsiAndSdkVersionReleaseTime.getBytes();

            final int tokenFullLen = Objects.isNull(token) ? 4 : token.length + 4;
            final int serviceCmdFullLen = serviceCmdBytes.length + 4;
            final int cookieFullLen = cookie.length + 4;
            final int imeiFullLen = imei.length + 4;
            final int ksidFullLen = ksid.length + 4;
            final int stringFullLen = stringBytes.length + 2;

            final int len = 4 + 24 + tokenFullLen + serviceCmdFullLen + cookieFullLen + imeiFullLen + ksidFullLen + stringFullLen;
            ByteBuffer buf = ByteBuffer.allocate(len);
            buf.putInt(len);
            buf.putInt(requestId);
            buf.putInt(appId);
            buf.putInt(appId);
            buf.putInt(0x71000000);
            buf.putInt(0);
            buf.putInt(0);
            buf.putInt(tokenFullLen);
            if (Objects.nonNull(token)) {
                buf.put(token);
            }
            buf.putInt(serviceCmdFullLen);
            buf.put(serviceCmdBytes);
            buf.putInt(cookieFullLen);
            if (Objects.nonNull(cookie)) {
                buf.put(cookie);
            }
            buf.putInt(imeiFullLen);
            buf.put(imei);
            buf.putInt(ksidFullLen);
            if (Objects.nonNull(ksid)) {
                buf.put(ksid);
            }
            buf.putShort((short)stringFullLen);
            buf.put(stringBytes);

            return buf.array();
        }
    }
    public int getRequestId() {
        return requestId;
    }

    public void setCookie(byte[] cookie) {
        this.cookie = cookie;
    }
}
